<template>
  <div>
    <div>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="企业名">
          <el-input
            v-model="formInline.epname"
            prefix-icon="el-icon-search"
            placeholder="企业名"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 分割线 -->
    <div>
      <el-divider><i class="el-icon-s-order" /></el-divider>
    </div>

    <!-- 表格 -->
    <div>
      <el-table :data="pageBean.pageBeanList" border style="width: 100%">
        <el-table-column type="index" label="序号" width="80">
        </el-table-column>
        <el-table-column prop="epname" label="企业名称" width="250">
        </el-table-column>
        <el-table-column prop="epcode" label="社会信用代码" width="250">
        </el-table-column>
        <el-table-column prop="epcapital" label="注册资金" width="180">
        </el-table-column>
        <el-table-column prop="epetime" label="企业成立时间"> </el-table-column>
        <el-table-column fixed="right" label="操作" width="180">
          <template slot-scope="scope">
            <el-button
              @click="updateStateClick(scope.row)"
              type="text"
              size="small"
              >移除黑名单</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页插件 -->
    <div class="block">
      <div>
        <el-divider></el-divider>
      </div>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageBean.pageIndex"
        :page-sizes="[2, 3, 20, 30]"
        :page-size="pageBean.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageBean.totalRecord"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      formInline: {
        epname: "",
      },
      epstate: 2,
      pageBean: {
        pageBeanList: [],
        pageIndex: 1,
        pageSize: 2,
        totalRecord: 0,
        totalPage: 0,
      },
    };
  },
  created() {
    this.allepinfo();
  },
  methods: {
    allepinfo() {
      this.$axios
        .get("http://localhost:8082/enp/allenp", {
          params: {
            epname: this.formInline.epname,
            epstate: this.epstate,
            pageIndex: this.pageBean.pageIndex,
            pageSize: this.pageBean.pageSize,
          },
        })
        .then((resp) => {
          console.log(resp);
          this.pageBean = resp.data.data;
        });
    },

    onSubmit() {
      console.log("submit!");
      this.allepinfo();
    },

    updateStateClick(row) {
      this.$confirm("是否将该客户移除黑名单?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let epstate = 1;
          this.$axios
            .put(
              "http://localhost:8082/enp/updateEpstate/" +
                row.epid +
                "/" +
                epstate
            )
            .then((r) => {
              console.log("修改成功");
              //   解决当前页数据移除完时，任停留在当前页，无数据、不刷新
              if (this.pageBean.pageIndex > 1) {
                this.pageBean.pageIndex =
                  (this.pageBean.totalRecord - 1) % this.pageBean.pageSize > 0
                    ? this.pageBean.pageIndex
                    : this.pageBean.pageIndex - 1;
              }

              this.allepinfo();
            });
          this.$message({
            type: "success",
            message: "已移除黑名单!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },

    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageBean.pageSize = val;
      this.allepinfo();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageBean.pageIndex = val;
      this.allepinfo();
    },
  },
};
</script>

<style>
</style>